import Mock from 'mockjs';

let users = [
  { id: 1, userName: 'zs', password: '123123!Q', role: 'admin' },
  { id: 2, userName: 'gy', password: '123123!Q', role: 'admin' },
  { id: 3, userName: 'qmm', password: '123123!Q', role: 'user' },
  { id: 4, userName: 'cml', password: '123123!Q', role: 'user' },
];

let data = {};
// 注册
const registerData = (req) => {
  let { userName, password } = JSON.parse(req.body);
  let isHas = users.some((user) => user.userName == userName);
  if (isHas) {
    data = {
      status: 400,
      msg: '当前账号已注册',
    };
    return data;
  } else {
    users.push({ userName: userName, password: password, role: 'user' });
    data = {
      status: 200,
      msg: '注册成功',
    };
    return data;
  }
};

// 登录
const loginData = (req) => {
  let { userName, password } = JSON.parse(req.body);
  let isHas = users.some((user) => user.userName == userName);
  let isTrue = users.some((user) => user.userName == userName && user.password == password);
  if (!isHas) {
    data = {
      status: 400,
      msg: '当前账号未注册',
    };
    return data;
  } else if (!isTrue) {
    data = {
      status: 400,
      msg: '账号或密码错误',
    };
    return data;
  } else {
    data = {
      status: 200,
      msg: '登录成功',
    };
    return data;
  }
};

// 获取用户列表
const usersData = (req) => {
  let userName = req.url.split('=')[1];
  let role = users.filter((user) => user.userName === userName)[0].role;
  let usersArr = [];
  if (role === 'user') {
    usersArr = users.filter((user) => user.role === role);
  } else {
    usersArr = users;
  }

  data = {
    status: 200,
    msg: '数据加载成功',
    usersList: usersArr,
    role,
  };
  return data;
};

// 添加用户
const addData = (req) => {
  let { userName, role } = JSON.parse(req.body);
  let isHas = users.some((user) => user.userName == userName);
  if (!isHas) {
    users.push({ userName, role, id: users.length + 1 });
    data = {
      status: 200,
      msg: '添加成功',
    };
  } else {
    data = {
      status: 400,
      msg: '用户已存在',
    };
  }
  return data;
};

// 删除用户
const deleteData = (req) => {
  let userName = req.url.split('=')[1];
  let index = 0;
  for (let i = 0; i < users.length; i++) {
    if (users[i].userName === userName) {
      index = i;
      break;
    }
  }
  users.splice(index, 1);
  data = {
    status: 200,
    msg: '删除成功',
  };
  return data;
};

// 修改用户
const editData = (req) => {
  let { userName, id } = JSON.parse(req.body);
  for (let i = 0; i < users.length; i++) {
    if (users[i].id === id) {
      users[i].userName = userName;
      break;
    }
  }
  data = {
    status: 200,
    msg: '更新成功',
  };
  return data;
};

// 查找用户
const findData = (req) => {
  let userName = req.url.split('=')[1];
  let userArr = [];
  userArr = users.filter((user) => user.userName === userName);
  data = {
    status: 200,
    msg: '查找成功',
    userList: userArr,
  };
  return data;
};

// api流量数据
const apiData = () => {
  let apiData = [
    { TopTime: 0, TopFlow: 3000 },
    { TopTime: 1, TopFlow: 3100 },
    { TopTime: 2, TopFlow: 3500 },
    { TopTime: 3, TopFlow: 2000 },
    { TopTime: 4, TopFlow: 1000 },
    { TopTime: 5, TopFlow: 1000 },
    { TopTime: 6, TopFlow: 1000 },
    { TopTime: 7, TopFlow: 1000 },
    { TopTime: 8, TopFlow: 1000 },
    { TopTime: 9, TopFlow: 600 },
    { TopTime: 10, TopFlow: 1000 },
    { TopTime: 11, TopFlow: 1000 },
    { TopTime: 12, TopFlow: 1000 },
    { TopTime: 13, TopFlow: 1000 },
    { TopTime: 14, TopFlow: 1000 },
    { TopTime: 15, TopFlow: 1000 },
    { TopTime: 16, TopFlow: 1000 },
    { TopTime: 17, TopFlow: 1000 },
    { TopTime: 18, TopFlow: 1000 },
    { TopTime: 19, TopFlow: 2000 },
    { TopTime: 20, TopFlow: 2000 },
    { TopTime: 21, TopFlow: 1000 },
    { TopTime: 22, TopFlow: 1000 },
    { TopTime: 23, TopFlow: 1000 },
  ];
  data = {
    apiData,
    status: 200,
    msg: '获取数据成功',
  };

  return data;
};

Mock.mock('/api/register', 'post', registerData);
Mock.mock('/api/login', 'post', loginData);
Mock.mock('/api/addUser', 'post', addData);

Mock.mock('/api/editUser/', 'put', editData);

Mock.mock(/\/api\/findUser/, 'get', findData);

Mock.mock(/\/api\/getAPIData/, 'get', apiData);

// delete 同
Mock.mock(/\/api\/deleteUser*?/, 'delete', deleteData);

// get mock 用正则
Mock.mock(/\/api\/users*?/, 'get', usersData);
